package com.szw.dlyk.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szw.dlyk.constant.Constant;
import com.szw.dlyk.domain.Activity;
import com.szw.dlyk.domain.User;
import com.szw.dlyk.query.ActivityQuery;
import com.szw.dlyk.query.BaseQuery;
import com.szw.dlyk.service.ActivityService;
import com.szw.dlyk.mapper.ActivityMapper;
import com.szw.dlyk.util.JwtUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author asus
* @description 针对表【t_activity(市场活动表)】的数据库操作Service实现
* @createDate 2024-01-21 18:16:50
*/
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity>
    implements ActivityService{

    @Resource
    private ActivityMapper activityMapper;

    @Override
    public PageInfo<Activity> getActivityByPage(Integer current, ActivityQuery activityQuery) {
        // 1.设置PageHelper
        PageHelper.startPage(current, Constant.PAGE_SIZE);
        // 2.查询
        List<Activity> list = activityMapper.selectActivityByPage(activityQuery);
        // 3.封装分页数据到PageInfo
        PageInfo<Activity> info = new PageInfo<>(list);

        return info;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int saveActivity(ActivityQuery activityQuery) {
        Activity activity = new Activity();

        //把ActivityQuery对象里面的属性数据复制到TActivity对象里面去(复制要求：两个对象的属性名相同，属性类型要相同，这样才能复制)
        BeanUtils.copyProperties(activityQuery, activity);

        activity.setCreateTime(new Date()); //创建时间

        //登录人的id
        Integer loginUserId = JwtUtil.parseUserFromJWT(activityQuery.getToken()).getId();
        activity.setCreateBy(loginUserId); //创建人

        return activityMapper.insertSelective(activity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateActivity(ActivityQuery activityQuery) {
        Activity activity = new Activity();

        //把ActivityQuery对象里面的属性数据复制到TActivity对象里面去(复制要求：两个对象的属性名相同，属性类型要相同，这样才能复制)
        BeanUtils.copyProperties(activityQuery, activity);

        activity.setEditTime(new Date()); //创建时间

        //登录人的id
        Integer loginUserId = JwtUtil.parseUserFromJWT(activityQuery.getToken()).getId();
        activity.setEditBy(loginUserId); //创建人

        return activityMapper.updateByPrimaryKeySelective(activity);
    }

    @Override
    public Activity getActivityById(Integer id) {
      return   activityMapper.selectDetailByPrimaryKey(id);
    }

    @Override
    public List<Activity> getOngoingActivity() {
        return activityMapper.selecOngoingActivity();
    }
}




